//
//  AirInfoViewController.m
//  Conference
//
//  Created by 陈鑫 on 12-11-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AirInfoViewController.h"
#import "FlightCellController.h"
#import "AirDetailViewController.h"
#import "FlightData.h"
#import "MainDataBase.h"
#import "NetContant.h"
#import "Globals.h"
#import "Constant.h"
#import "WebData.h"
#import "DateTool.h"
@interface AirInfoViewController ()

@end

@implementation AirInfoViewController
@synthesize lab_title;
@synthesize table;
@synthesize arrayOfData;
@synthesize airType;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    if ([self.airType isEqualToString:@"1"]) {
        self.lab_title.text=@"接机安排";
    }else if ([self.airType isEqualToString:@"2"]) {
        self.lab_title.text=@"送机安排";
    }else {
        return;
    }
    
    [self loadingData];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(void)loadingData{
    [self getDataFromDb];
    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
    [thread start];

}
-(void)getDataFromDb{
    if(self.arrayOfData!=nil||[self.arrayOfData count]>0)
        [self.arrayOfData removeAllObjects];
    self.arrayOfData=[[MainDataBase getInstance]getFlight:self.airType];

}

-(void)run{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sRet=@"";
    if([@"1" isEqualToString:self.airType]){
        sRet=[[WebData getInstance]getFlight:self.airType preUpdate:[Globals getInstance].sPickUpUpdate];
    }
    else if([@"2" isEqualToString:self.airType]){
    sRet=[[WebData getInstance]getFlight:self.airType preUpdate:[Globals getInstance].sSeeOffUpdate];
    }
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
      //  [[Globals getInstance] showAlertView:[[Constant getInstance] net_error]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
       // [[Globals getInstance] showAlertView:[[Constant getInstance] data_isempty]];
        
    }else {
        @try {
            [self doNetRet:sRet];
            [self doNetInfo];
        }
        @catch (NSException *exception) {
            //
        }
        
        
    }
    
}
-(void)doNetRet:(NSString *)sRet{
    NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
    
    NSMutableArray *dataList=[NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<[tables count]; i++) {
        if(nil!=[tables objectAtIndex:i]){
            NSArray *record=[[tables objectAtIndex:i] componentsSeparatedByString:[[NetContant getInstance] RECORDS_SPLIT_FLAG]];
            if([record count]>=6){
                FlightData *flightData=[[FlightData alloc]init];
                flightData.ID=[record objectAtIndex:0];
                flightData.title = [record objectAtIndex:1];
                flightData.dateTime = [record objectAtIndex:2];
                flightData.schedule=[record objectAtIndex:3];
                flightData.detail=[record objectAtIndex:4];
                flightData.notes=[record objectAtIndex:5];
                flightData.type=self.airType;
                [dataList addObject:flightData];
                [flightData release];
            }
        }
    }
    
    [[MainDataBase getInstance] addFlight:dataList];
    
    
    if ([@"1" isEqualToString:self.airType])[Globals getInstance].sPickUpUpdate=[NSString stringWithFormat:@"%@ %@",[[DateTool getInstance]getNowDate],[[DateTool getInstance]getNowTime]];
    else if ([@"2" isEqualToString:self.airType])[Globals getInstance].sSeeOffUpdate=[NSString stringWithFormat:@"%@ %@",[[DateTool getInstance]getNowDate],[[DateTool getInstance]getNowTime]];
}

-(void)doNetInfo{
    [self getDataFromDb];
    [self.table reloadData];

}
- (void)viewDidUnload
{
    [self setAirType:nil];
    [self setArrayOfData:nil];
    [self setLab_title:nil];
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    return 0;
    return [self.arrayOfData count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *flightCell=@"flightCell";
    FlightCellController *cell=(FlightCellController *)[tableView dequeueReusableCellWithIdentifier:flightCell];
    if(cell ==nil){
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FlightCellController" owner:self options:nil];
		for (id oneObject in array) {
			if ([oneObject isKindOfClass:[FlightCellController class]]) {
				cell=oneObject;
				break;
			}
		}
        cell.selectionStyle=UITableViewCellSelectionStyleBlue;
    }
    FlightData  *data=[self.arrayOfData objectAtIndex:[indexPath row]];
    cell.lab_content.text=data.title;
    
    if([indexPath row] == self.arrayOfData.count-1)
        [cell.img_split_line setHidden:YES];
    else {
        [cell.img_split_line setHidden:NO];
    }
   
    return cell;
    
}

-(void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AirDetailViewController *mAirDetailViewController=[[AirDetailViewController alloc]initWithNibName:@"AirDetailViewController" bundle:nil];
    mAirDetailViewController.flightData=[self.arrayOfData objectAtIndex:[indexPath row]];
    mAirDetailViewController.airType=[self.lab_title.text isEqualToString:@"接机安排"]?1:2;
    [self.navigationController pushViewController:mAirDetailViewController animated:YES];
    [mAirDetailViewController release];
    [tableView1 deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [airType release];
    [arrayOfData release];
    [lab_title release];
    [table release];
    [super dealloc];
}
- (IBAction)btnBackAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
